﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using PARK.Common.Core.Manager;
using PARK.Common.Enum;
using PARK.Common.Filter;
using PARK.Consumer.Entity;
using PARK.Consumer.Entitys.Dto.ConsumMenus;
using PARK.Consumer.Entitys.Dto.ConsumtimeInterval;
using PARK.Consumer.Interfaces;
using PARK.Dependency;
using PARK.DynamicApiController;
using PARK.FriendlyException;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;

namespace PARK.Consumer
{
    /// <summary>
    /// 食堂菜单
    /// </summary>
    [ApiDescriptionSettings(Tag = "Consum", Name = "ConsumMenus", Order = 200)]
    [Route("api/consum/[controller]")]
    public class ConsumMenusService : IConsumMenusService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<ConsumMenusEntity> _consumtimeIntervalRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;

        /// <summary>
        /// 初始化一个<see cref="ConsumMenusService"/>类型的新实例
        /// </summary>
        public ConsumMenusService(
            ISqlSugarRepository<ConsumMenusEntity> consumtimeIntervalRepository,
            IUserManager userManager)
        {
            _consumtimeIntervalRepository = consumtimeIntervalRepository;
            _db = _consumtimeIntervalRepository.Context;
            _userManager = userManager;
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<dynamic> GetInfo(string id)
        {
            var entity = await _db.Queryable<ConsumMenusEntity>().FirstAsync(p => p.Id == id);
            return entity;
        }
        /// <summary>
		/// 获取食堂菜单列表
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
        [HttpGet("")]
        public async Task<dynamic> GetList([FromQuery] ConsumMenusListQueryInput input)
        {
            var userInfo = await _userManager.GetUserInfo();
            if (userInfo != null && userInfo.UserAdminFlag != 2 && !string.IsNullOrEmpty(userInfo.companyId))
            {
                var sidx = input.sidx == null ? "sortcode" : input.sidx;
                var data = await _db.Queryable<ConsumMenusEntity>()
                    //公司权限
                    .Where(p => p.CompanyId.Equals(userInfo.companyId))
                    .WhereIF(!string.IsNullOrEmpty(input.keyword), p => p.TimeIntervalName.Contains(input.keyword))
                    .Select(it => new ConsumMenusListOutput
                    {
                        id = it.Id,
                        timeIntervalName = it.TimeIntervalName,
                        week1 =it.Week1,
                        week2 = it.Week2,
                        week3 = it.Week3,
                        week4 = it.Week4,
                        week5 = it.Week5,
                        week6 = it.Week6,
                        week7 = it.Week7,
                        CompanyId = it.CompanyId,
                        CompanyName = it.CompanyName,
                        sortcode =it.SortCode,
                    }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
                return PageResult<ConsumMenusListOutput>.SqlSugarPageResult(data);
            }
            else
                return null;
        }

        /// <summary>
        /// 更新食堂菜单
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPut("{Id}")]
        public async Task Update(string Id, [FromBody] ConsumMenusUpInput  input)
        {
            var userInfo = await _userManager.GetUserInfo();
            if (userInfo != null && userInfo.UserAdminFlag != 2 && !string.IsNullOrEmpty(userInfo.companyId))
            {
                var entity = input.Adapt<ConsumMenusEntity>();
                entity.CompanyId = userInfo.companyId;
                entity.CompanyName = userInfo.companyName;
                entity.LastModify(Id);
                var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
                if (!(isOk > 0)) throw PARKException.Oh(ErrorCode.COM1001);
            }
            else
                throw PARKException.Bah("您的权限无法操作!");
        }

    }
}

